﻿#include "MirLabel.h"
#include "../Forms/Client.CMain.h"
#include "../Settings.h"

using namespace Client::MirGraphics;
using namespace SlimDX;
using namespace SlimDX::Direct3D9;

namespace Client::MirControls
{

    bool MirLabel::getAutoSize() const
    {
        return _autoSize;
    }

    void MirLabel::setAutoSize(bool value)
    {
        if (_autoSize == value)
        {
            return;
        }
        _autoSize = value;
        OnAutoSizeChanged(EventArgs::Empty);
    }

    void MirLabel::OnAutoSizeChanged(EventArgs *e)
    {
        TextureValid = false;
        GetSize();
        if (AutoSizeChanged != nullptr)
        {
            AutoSizeChanged::Invoke(this, e);
        }
    }

    TextFormatFlags *MirLabel::getDrawFormat() const
    {
        return _drawFormat;
    }

    void MirLabel::setDrawFormat(TextFormatFlags *value)
    {
        _drawFormat = value;
        OnDrawFormatChanged(EventArgs::Empty);
    }

    void MirLabel::OnDrawFormatChanged(EventArgs *e)
    {
        TextureValid = false;

        if (DrawFormatChanged != nullptr)
        {
            DrawFormatChanged::Invoke(this, e);
        }
    }

    System::Drawing::Font *MirLabel::getFont() const
    {
        return _font;
    }

    void MirLabel::setFont(System::Drawing::Font *value)
    {
        _font = ScaleFont(value);
        OnFontChanged(EventArgs::Empty);
    }

    void MirLabel::OnFontChanged(EventArgs *e)
    {
        TextureValid = false;

        GetSize();

        if (FontChanged != nullptr)
        {
            FontChanged::Invoke(this, e);
        }
    }

    bool MirLabel::getOutLine() const
    {
        return _outLine;
    }

    void MirLabel::setOutLine(bool value)
    {
        if (_outLine == value)
        {
            return;
        }
        _outLine = value;
        OnOutLineChanged(EventArgs::Empty);
    }

    void MirLabel::OnOutLineChanged(EventArgs *e)
    {
        TextureValid = false;
        GetSize();

        if (OutLineChanged != nullptr)
        {
            OutLineChanged::Invoke(this, e);
        }
    }

    Color *MirLabel::getOutLineColour() const
    {
        return _outLineColour;
    }

    void MirLabel::setOutLineColour(Color *value)
    {
        if (_outLineColour == value)
        {
            return;
        }
        _outLineColour = value;
        OnOutLineColourChanged();
    }

    void MirLabel::OnOutLineColourChanged()
    {
        TextureValid = false;

        if (OutLineColourChanged != nullptr)
        {
            OutLineColourChanged::Invoke(this, EventArgs::Empty);
        }
    }

    void MirLabel::GetSize()
    {
        if (!getAutoSize())
        {
            return;
        }

        if (_text.empty())
        {
            setSize(getSize()->Empty);
        }
        else
        {
            setSize(TextRenderer::MeasureText(CMain::Graphics, getText(), getFont()));
            //Size = new Size(Size.Width, Size.Height + 5);

            if (getOutLine() && getSize() != getSize()->Empty)
            {
                Size tempVar(getSize()->Width + 2, getSize()->Height + 2);
                setSize(&tempVar);
            }
        }
    }

    std::string MirLabel::getText() const
    {
        return _text;
    }

    void MirLabel::setText(const std::string &value)
    {
        if (_text == value)
        {
            return;
        }

        _text = value;
        OnTextChanged(EventArgs::Empty);
    }

    void MirLabel::OnTextChanged(EventArgs *e)
    {
        setDrawControlTexture(!getText().empty());
        TextureValid = false;
        Redraw();

        GetSize();

        if (TextChanged != nullptr)
        {
            TextChanged::Invoke(this, e);
        }
    }

    MirLabel::MirLabel()
    {
        setDrawControlTexture(true);
        _drawFormat = TextFormatFlags::WordBreak;

        System::Drawing::Font tempVar(Settings::FontName, 8.0F);
        _font = ScaleFont(&tempVar);
        _outLine = true;
        _outLineColour = Color::Black;
        _text = "";

    }

    void MirLabel::Dispose(bool disposing)
    {
        MirControl::Dispose(disposing);

        if (!disposing)
        {
            return;
        }

        AutoSizeChanged->addListener("nullptr", nullptr);
        _autoSize = false;

        DrawFormatChanged->addListener("nullptr", nullptr);
        _drawFormat = 0;

        FontChanged->addListener("nullptr", nullptr);
        _font = nullptr;

        OutLineChanged->addListener("nullptr", nullptr);
        _outLine = false;

        OutLineColourChanged->addListener("nullptr", nullptr);
        _outLineColour = Color::Empty;

        TextChanged->addListener("nullptr", nullptr);
        _text = "";
    }
}
